Object.defineProperty(exports, "__esModule", {
  value: !0
}), exports.codeSearchCity = void 0, exports.formatDistanceUnits = r, exports.getDistance = function (e, t, o, n) {
  var i = Math.PI;

  function a(e) {
    return e * i / 180
  }
  var s = a((e + o) / 2),
    c = a((e - o) / 2),
    d = a((t - n) / 2),
    u = Math.sin(c),
    l = Math.sin(d),
    p = Math.sin(s),
    f = (u *= u) * (1 - (l *= l)) + (1 - (p *= p)) * l,
    g = (1 - u) * (1 - l) + p * l,
    v = Math.atan(Math.sqrt(f / g)),
    h = Math.sqrt(f * g) / v;
  return r(2 * v * 6378137 * (1 + 1 / 298.257 * ((3 * h - 1) / 2 / g * p * (1 - u) - (3 * h + 1) / 2 / f * (1 - p) * u)) || 0)
}, exports.getSearchLocation = exports.getLocation = void 0;
var e = require("../../../@babel/runtime/helpers/objectSpread2"),
  t = require("../../../store/index"),
  o = (require("../../../utils/index").helper.location, "20f12aae660c04de86f993d3eff590a0"),
  n = {
    key: o,
    s: "rsx",
    platform: "WXJS",
    appname: o,
    sdkversion: "1.2.0",
    logversion: "2.0"
  };

function i(o) {
  function i(t) {
    var i = e({
      location: t,
      extensions: "all"
    }, n);
    wx.request({
      url: "https://restapi.amap.com/v3/geocode/regeo",
      data: i,
      method: "GET",
      header: {
        "content-type": "application/json"
      },
      success: function (e) {
        if (console.log("高德api请求结果 :>> ", e), e.data.status && "1" == e.data.status) {
          var n, i, a = e.data.regeocode,
            r = a.addressComponent,
            s = [],
            c = null !== (n = a.roads[0]) && void 0 !== n && n.name ? "".concat(null === (i = a.roads[0]) || void 0 === i ? void 0 : i.name, "附近") : r.township.length ? r.township : a.formatted_address,
            d = t.split(",")[0],
            u = t.split(",")[1];
          a.pois && a.pois[0] && (c = "".concat(a.pois[0].name, "附近")), r.provice && s.push(r.provice), r.city && s.push(r.city), r.district && s.push(r.district), r.streetNumber && r.streetNumber.street && r.streetNumber.number ? (s.push(r.streetNumber.street), s.push(r.streetNumber.number)) : s.push(a.roads[0].name);
          var l = s.join("");
          o.success({
            name: l,
            desc: c,
            longitude: d,
            latitude: u,
            regeocodeData: a
          })
        } else o.fail({
          errCode: e.data.infocode,
          errMsg: e.data.info
        })
      },
      fail: function (e) {
        console.log("高德请求失败 :>> ", e), o.fail({
          errCode: e.errCode,
          errMsg: e.errMsg || ""
        })
      }
    })
  }
  o.location ? i(o.location) : function (e, o) {
    wx.getLocation({
      type: "gcj02",
      success: function (e) {
        console.log("getWxLocation success:>> ", e);
        var n = "".concat(e.longitude, ",").concat(e.latitude);
        (0, t.dispatch)(t.actions.storageActions.setItem({
          key: "userLocation",
          value: n
        })), o(n)
      },
      fail: function (e) {
        console.log("getWxLocation fail:>> ", e), wx.getStorage({
          key: "userLocation",
          success: function (e) {
            e.data && o(e.data)
          },
          fail: function () {
            o("116.397554,39.906928")
          }
        })
      }
    })
  }(0, (function (e) {
    i(e)
  }))
}

function a(t) {
  var o = t.keywords,
    i = t.city,
    a = e({
      citylimit: !0,
      city: i,
      keywords: o
    }, n);
  wx.request({
    url: "https://restapi.amap.com/v3/assistant/inputtips",
    data: a,
    method: "GET",
    header: {
      "content-type": "application/json"
    },
    success: function (o) {
      o && o.data && o.data.tips ? t.success({
        tips: o.data.tips
      }) : t.fail(e({
        errCode: "0",
        errMsg: o.errMsg || ""
      }, o))
    },
    fail: function (e) {
      t.fail({
        errCode: "0",
        errMsg: e.errMsg || ""
      })
    }
  })
}

function r(e) {
  var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 1;
  return e <= 0 ? "" : e < 999 ? "".concat(Math.ceil(e), "m") : "".concat((e / 1e3).toFixed(t < 0 ? 2 : t), "km")
}
exports.getSearchLocation = function () {
  var t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
  return new Promise((function (o, n) {
    a(e({
      success: function (e) {
        var t = e.tips.filter((function (e) {
          var t;
          return e.adcode && (null === (t = e.location) || void 0 === t ? void 0 : t.length)
        }));
        o(t)
      },
      fail: function (e) {
        console.log("getInputTips fail:>> ", e), n(e)
      }
    }, t))
  }))
}, exports.getLocation = function () {
  var t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
  return new Promise((function (o, n) {
    i(e({
      success: function (e) {
        var t, n, i, a = null === (t = e.regeocodeData) || void 0 === t || null === (n = t.addressComponent) || void 0 === n || null === (i = n.neighborhood) || void 0 === i ? void 0 : i.name,
          r = e.regeocodeData.addressComponent.adcode,
          s = Object.assign(e, {
            pois: e.regeocodeData.pois,
            address: Array.isArray(a) ? e.desc : a,
            adcode: r
          });
        o(s)
      },
      fail: function (e) {
        console.log("getRegeo fail:>> ", e), n(e)
      }
    }, t))
  }))
};
exports.codeSearchCity = function (e) {
  var o = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : t.store.getState().storage.areaTree,
    n = {
      adcode: e,
      province_id: "",
      city_id: "",
      city_text: "",
      province_text: ""
    },
    i = "",
    a = o.find((function (e) {
      return i = e.children.find((function (e) {
        var t = "".concat(n.adcode.slice(0, -2), "00"),
          o = "".concat(e.adcode.slice(0, -2), "00");
        return o == t && (n.city_id = e.id, n.province_id = e.pid, n.city_text = e.name), o == t
      }))
    }));
  return n.province_text = null == a ? void 0 : a.name, {
    params: n,
    city: i,
    province: a
  }
};